<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class express_uqcms extends control
{
    function __construct()
    {
        $this->spid = @$_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function index()
    {
        $query = $this->db->get_all("select * from " . table('shop_express') . " where shop_id = " . $this->spid . " order by id asc");
        if ($query) {
            foreach ($query as $v) {
                $v['addtime'] = _date($v['addtime']);
                $data[] = $v;
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function edit()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('shop_express') . " where shop_id = " . $this->spid . " and id = " . $id);
            if ($row['id']) {
                $this->assign('row', $row);
            }
        }
        $area_all = $this->db->get_all("select * from " . table('area') . " where pid = 0");
        if ($area_all) {
            foreach ($area_all as $v) {
                $area[] = $v;
            }
            $this->assign('area', $area);
        }
        $this->display();
    }
    public function save()
    {
        if ($_POST) {
            $setsql['shop_id'] = $this->spid;
            $setsql['name'] = !empty($_POST['name']) ? $_POST['name'] : error_json('模板名称不能为空');
            $setsql['price'] = !empty($_POST['price']) ? $_POST['price'] : '0.00';
            $setsql['status'] = !empty($_POST['status']) ? $_POST['status'] : '1';
            $setsql['addtime'] = time();
            $id = !empty($_POST['id']) ? $_POST['id'] : false;
            if ($id) {
                $query = $this->db->update(table('shop_express'), $setsql, 'shop_id = ' . $this->spid . ' and id = ' . $id);
            } else {
                $query = $this->db->add(table('shop_express'), $setsql);
            }
            if ($query) {
                right_json();
            } else {
                error_json('添加失败');
            }
        }
    }
    public function del()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : showmsg('提交错误');
        $query = $this->db->delete(table('shop_express'), 'shop_id = ' . $this->spid . ' and id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    public function get_pro()
    {
        $code = !empty($_GET['code']) ? $_GET['code'] : exit('快递号不能为空');
        $number = !empty($_GET['nu']) ? $_GET['nu'] : exit('订单号不能为空');
        $result = $this->module('express')->get_express_pro($code, $number);
        if ($result) {
            foreach ($result as $k => $v) {
                $data[] = array('msg' => $v->msg, 'time' => $v->time);
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
}